package com.ygs.secs.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ygs.secs.model.Enterprise;
import com.ygs.secs.model.EnterpriseJob;
import com.ygs.secs.model.Record;
import com.ygs.secs.service.AuthInfomationService;
import com.ygs.secs.service.EnterpriseJobService;
import com.ygs.secs.service.EnterpriseService;
import com.ygs.secs.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生招聘管理控制器
 */
@RestController
@RequestMapping("/ygssecs")
public class StudentRecruitmentController {
    @Autowired
    EnterpriseJobService jobService;
    @Autowired
    AuthInfomationService authService;
    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    RecordService recordService;

    /**
     * 获得岗位列表(最新岗位)
     *
     * @return 通用json返回格式
     */
    @RequestMapping("/getjobList")
    @ResponseBody
    public Map<String, Object> getJobList() {
        Map<String, Object> map = new HashMap();
        List<EnterpriseJob> jobList = jobService.getjobList();
        Map<String, Object> datamap = new HashMap();
        if (jobList != null) {
            if (jobList.size() > 0) {
                map.put("code", 200);
                map.put("msg", "岗位数据请求成功");
                datamap.put("list", jobList);
                map.put("data", datamap);
            } else {
                map.put("code", 200);
                map.put("msg", "还没有岗位数据");
                datamap.put("list", jobList);
                map.put("data", datamap);
            }

        } else {
            map.put("code", 500);
            map.put("msg", "岗位数据请求失败");
            datamap.put("list", null);
            map.put("data", datamap);
        }
        return map;
    }

    /**
     * 获取前最新认证注册企业
     *
     * @return josn
     */
    @RequestMapping("/getEnterpriseTop12")
    public Map<String, Object> getEnterpriseTop12() {
        Map<String, Object> map = new HashMap<>();
        try {
            Map<String, Object> datamap = new HashMap<>();
            List<Enterprise> enterpriseList = enterpriseService.getlatestEnterpriseList();
            if (enterpriseList != null) {
                datamap.put("list", enterpriseList);
                map.put("code", 200);
                map.put("msg", "数据请求成功");
                map.put("data", datamap);
            } else {
                map.put("code", 500);
                map.put("msg", "数据请求失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据请求失败");
            map.put("data", null);
        }
        return map;
    }

    /**
     * 岗位推荐列表
     *
     * @return 通用json格式
     */
    @RequestMapping("/getRecommendjoblist")
    public Map<String, Object> getRecommendjoblist(String postionnum) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<EnterpriseJob> jobList = jobService.getRecommendjoblist(Integer.parseInt(postionnum));
            Map<String, Object> datamap = new HashMap();
            if (jobList != null) {
                datamap.put("list", jobList);
                map.put("code", 200);
                map.put("msg", "数据请求成功");
                map.put("data", datamap);
            } else {
                map.put("code", 500);
                map.put("msg", "数据请求失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据请求失败");
            map.put("data", null);
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 获取明星企业
     *
     * @return 通用json格式
     */
    @RequestMapping("/getfamousEnterpriseList")
    public Map<String, Object> getfamousEnterpriseList() {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> datamap = new HashMap<>();
        try {
            List<Enterprise> enterpriseList = enterpriseService.getfamousEnterpriseList();
            if (enterpriseList != null) {
                datamap.put("list", enterpriseList);
                map.put("code", 200);
                map.put("msg", "数据请求成功");
                map.put("data", datamap);
            } else {
                map.put("code", 500);
                map.put("msg", "数据请求失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据请求失败");
            map.put("data", null);
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 学生投递简历申请岗位
     *
     * @param recordjson 应聘记录中中间表json
     * @return josn
     */
    @RequestMapping("/applypostion")
    public Map<String, Object> applypostion(String recordjson) {
        Map<String, Object> map = new HashMap<>();
        try {
            Record record = JSONObject.parseObject(recordjson, Record.class);
            int checkpostionnum = recordService.checkpostionnum(record.getStudent().getStuid());
            if (checkpostionnum > 20) {
                map.put("code", 201);
                map.put("msg", "投递次数超出限制了哦");
            } else {
                int i = recordService.insertSelective(record);
                if (i > 0) {
                    map.put("code", 200);
                    map.put("msg", "职位申请成功");
                } else {
                    map.put("code", 500);
                    map.put("msg", "职位申请失败");
                }
            }
        } catch (DuplicateKeyException e) {
            map.put("code", 201);
            map.put("msg", "不能重复申请哦");
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 检测投递状态
     *
     * @param stuid 学号
     * @param jobid 岗位id
     * @return 通用json
     */
    @RequestMapping("/detectionpostionStatus")
    public Map<String, Object> detectionpostionStatus(String stuid, String jobid) {
        Map<String, Object> map = new HashMap<>();
        try {
            Record record = recordService.checkrecord(stuid, jobid);
            if (record == null) {
                map.put("code", 200);
                map.put("status", 0);
            } else {
                map.put("code", 200);
                map.put("status", 1);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
        }
        return map;
    }

    /**
     * 相似岗位
     *
     * @return json
     */
    @RequestMapping("/similarPosition")
    public Map<String, Object> similarPosition(String jobtype) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> datamap = new HashMap<>();
        try {
            List<EnterpriseJob> jobList = jobService.getsimilarPositionlist(jobtype);
            if (jobList != null) {
                datamap.put("list", jobList);
                map.put("code", 200);
                map.put("msg", "数据请求成功");
                map.put("data", datamap);
            } else {
                map.put("code", 201);
                map.put("msg", "数据请求失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据请求失败");
            map.put("data", null);
        }
        return map;
    }


    /**
     * 根据条件查询岗位
     *
     * @param qt    查询前置条件(按公司或者职位查询)
     * @param kw    关键词
     * @param quiz1 条件1
     * @param quiz2 条件2
     * @return json
     */
    @RequestMapping("/queryjobbycondition")
    public Map<String, Object> queryjobbycondition(String qt, String kw, String quiz1, String quiz2, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "1") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        try {
            //分页查询
            Page startPage = PageHelper.startPage(pageNum, pageSize);
            List<EnterpriseJob> jobList;
            if (qt.equals("company")) {
                jobList = queryjoblistbycompany(kw);
                map.put("code", 200);
                map.put("msg", "数据查询成功");
                map.put("data", jobList);
                map.put("count", startPage.getTotal());
            } else {
                jobList = queryjobbyname(kw, quiz1, quiz2);
                map.put("code", 200);
                map.put("msg", "数据查询成功");
                map.put("data", jobList);
                map.put("count", startPage.getTotal());
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据查询失败");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 空条件查询
     *
     * @return
     */
    @RequestMapping("/queryjobbyEmptycondition")
    public Map<String, Object> queryjobbyEmptycondition() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<EnterpriseJob> jobList = queryjobrandom();
            map.put("code", 200);
            map.put("msg", "数据查询成功");
            map.put("data", jobList);
            map.put("count", jobList.size());
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据查询失败");
        }
        return map;
    }


    /**
     * 根据企业名称查询岗位数据
     *
     * @param kw 关键字
     * @return 岗位数据链表
     */
    private List<EnterpriseJob> queryjoblistbycompany(String kw) {
        return jobService.getjoblistbycommpanyname(kw);
    }

    /**
     * 随机查询岗位数据
     *
     * @return json
     */
    private List<EnterpriseJob> queryjobrandom() {
        return jobService.getRecommendjoblist(10);
    }

    /**
     * 根据岗位名称查询岗位数据
     *
     * @param kw 关键词
     * @return json
     */
    private List<EnterpriseJob> queryjobbyname(String kw, String qz1, String qz2) {
        return jobService.getjobListbyjobwithauthinfo(kw, qz1, qz2);
    }

}
